package com.ruoyi.web.controller.wechat.admin;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.wechat.WechatUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.user.UserException;
import com.ruoyi.system.domain.wechat.request.vo.CreateForWechatUserVO;
import com.ruoyi.system.domain.wechat.request.vo.FilterForWechatUserListVO;
import com.ruoyi.system.domain.wechat.request.vo.ModifyForWechatUserVO;
import com.ruoyi.system.domain.wechat.request.vo.ResetPwdForWechatUserVO;
import com.ruoyi.system.domain.wechat.response.WechatUserListVO;
import com.ruoyi.system.service.wechat.IWechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信小程序用户
 *
 * @author jiexiang.sun/Jason
 * @version 1.0.0
 * @create 2022-03-03 19:34
 **/
@RestController
@Validated
@RequestMapping("/admin/user")
public class AdminUserController extends BaseController {

    @Autowired
    private IWechatUserService wechatUserService;

    /**
     * 添加用户
     *
     * @param params
     * @return
     */
    @PostMapping("create")
    public AjaxResult create(@RequestBody CreateForWechatUserVO params) {
        return AjaxResult.success(wechatUserService.create(params));
    }

    /**
     * 修改用户
     *
     * @param params
     * @return
     */
    @PutMapping("modify")
    public AjaxResult modify(@RequestBody ModifyForWechatUserVO params) {
        return AjaxResult.success(wechatUserService.modify(params));
    }

    /**
     * 获取用户列表
     *
     * @param params
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(FilterForWechatUserListVO params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        return getDataTable(wechatUserService.list(params));
    }

    /**
     * 获取用户详情
     *
     * @return
     */
    @GetMapping("{userId}")
    public AjaxResult info(@PathVariable Long userId) {
        return AjaxResult.success(wechatUserService.info(userId));
    }

    /**
     * 用户账号审核
     *
     * @param param
     * @return
     */
    @PutMapping("audit")
    public AjaxResult audit(@RequestBody ModifyForWechatUserVO param) {
        return AjaxResult.success(wechatUserService.audit(param));
    }

    /**
     * 删除用户账号
     *
     * @param userIds
     * @return
     */
    @DeleteMapping("{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return AjaxResult.success(wechatUserService.remove(userIds));
    }

    /**
     * 拒绝用户删除申请
     * @param userId
     * @return
     */
    @PostMapping("reject/delete/{userId}")
    public AjaxResult rejectDelete(@PathVariable Long userId) {
        return AjaxResult.success(wechatUserService.rejectDelete(userId));
    }

    /**
     * 重置密码
     *
     * @param params
     * @return
     */
    @PutMapping("resetPwd")
    public AjaxResult resetPwd(@RequestBody ResetPwdForWechatUserVO params) {
        LambdaQueryWrapper<WechatUser> userQW = new LambdaQueryWrapper<>();
        userQW.eq(WechatUser::getUserId, params.getUserId());

        WechatUser user = wechatUserService.getOne(userQW);
        if (ObjectUtil.isNull(user)) {
            throw new UserException("wechat.user.not.exist", null);
        }

        if (!UserStatus.OK.getCode().equals(user.getDelFlag())) {
            throw new UserException("wechat.user.is.delete", null);
        }

        if (!UserStatus.OK.getCode().equals(user.getStatus())) {
            throw new UserException("wechat.user.verified.first", null);
        }

        LambdaUpdateWrapper<WechatUser> userUW = new LambdaUpdateWrapper<>();
        userUW.eq(WechatUser::getUserId, user.getUserId());

        user = new WechatUser();
        user.setPassword(SecureUtil.md5(params.getPassword()));
        return AjaxResult.success(wechatUserService.update(user, userUW));
    }
}
